package com.xkdx.caipiao.util;


import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.quert.IssueQueryInfo;
import com.xkdx.caipiao.presistence.quert.TypeQueryInfo;

public class SharePrefenceUtil {
	private static String TAG = "SharePrefenceUtil";

	Context context;
	SharedPreferences sp;
	SharedPreferences.Editor ep;

	public static String USERINFO = "userinfo";
	public static String LOTTERYTYPE = "lotteryType";
	public static String ISSUEQUERYINFO="issueQueryInfo";

	

	@SuppressWarnings("static-access")
	public SharePrefenceUtil(Context context, String name) {
		// Log.i(TAG, "-->" + name);
		this.context = context;
		if (context != null) {
			sp = context.getSharedPreferences(name, 0);
			ep = sp.edit();
		}
	}

	public void clear() {
		ep.clear();
		ep.commit();
	}

	public void saveObject(String key, Object obj) {
		String alibabaJson = JSON.toJSONString(obj);
		try {
			alibabaJson = Des3.encode(alibabaJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		ep.putString(key, alibabaJson);
		ep.commit();
	}

	public LoginInfo getUserInfo() {
		LoginInfo info = null;
		String alibabaJson = sp.getString(USERINFO, "");
		try {
			alibabaJson = Des3.decode(alibabaJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, LoginInfo.class);
		}
		return info;
	}

	public TypeQueryInfo getLotteryTypeInfo() {
		TypeQueryInfo info = null;
		String alibabaJson = sp.getString(LOTTERYTYPE, "");
		try {
			alibabaJson = Des3.decode(alibabaJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, TypeQueryInfo.class);
		}
		return info;
	}
	
	public IssueQueryInfo getIssueQueryInfo() {
		IssueQueryInfo info = null;
		String alibabaJson = sp.getString(ISSUEQUERYINFO, "");
		if (!alibabaJson.equals("")) {
			info = JSON.parseObject(alibabaJson, IssueQueryInfo.class);
		}
		return info;
	}
	
	

}
